package com.swg.wxorder.controller;

import com.swg.wxorder.result.ResultVOUtil;
import com.swg.wxorder.service.BuyerService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import com.swg.wxorder.converter.OrderForm2OrderDTOConverter;
import com.swg.wxorder.dto.OrderDTO;
import com.swg.wxorder.dto.OrderForm;
import com.swg.wxorder.enums.ResultEnum;
import com.swg.wxorder.exception.SellException;
import com.swg.wxorder.service.OrderService;
import com.swg.wxorder.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 【swg】.
 * @Date 2018/6/23 15:57
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    @RequestMapping("/create")
    public ResultVo<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult br) {
        //先进行数据的验证
        if (br.hasErrors()) {
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        OrderDTO orderDto = OrderForm2OrderDTOConverter.convert(orderForm);
        if(StringUtils.isEmpty(orderDto.getOrderDetailList())){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        //创建订单
        OrderDTO create = orderService.create(orderDto);
        //返回orderId给前端
        Map<String,String> map = new HashMap<>();
        map.put("orderId",create.getOrderId());
        return ResultVOUtil.success(map);

    }

    @RequestMapping("/detail")
    public ResultVo<OrderDTO> OrderDetail(@RequestParam("openid")String openid, @RequestParam("orderId") String orderId){
        //需要验证openid是否与订单中的openid一致，即检查是否是同一个人
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(orderDTO);
    }

    @RequestMapping("/list")
    public ResultVo<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size){
        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderDTO> orderDTOList = orderService.findList(openid,pageRequest);
        return ResultVOUtil.success(orderDTOList.getContent());
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        buyerService.cancleOrder(openid, orderId);
        return ResultVOUtil.success();
    }

    //取消订单
    @PostMapping("/finish")
    public ResultVo finish(@RequestParam("openid") String openid,
                           @RequestParam("orderid") String orderid) {
        buyerService.finishOrder(openid, orderid);
        return ResultVOUtil.success();
    }


}
